<template>
  <div class="app-container">
    <div>
      <avue-form
        :option="option"
        v-model="queryParams"
        ref="searchForm"
        v-show="showSearch"
        @keydown.enter="getList"
      >
        <template #menu-form>
          <el-button icon="Search" type="primary" @click="getList" class="m-l-3"
            >查询</el-button
          >
          <el-button icon="Refresh" @click="handleEmpty(searchForm)"
            >重置</el-button
          >
        </template>
      </avue-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="24" class="text-right">
          <el-button :icon="'Setting'" circle @click="dialogVisible = true" />
        </el-col>
      </el-row>

      <el-table
        v-loading="loading"
        :data="tableList"
        row-key="id"
        stripe
        border
        row-class-name="cursor-pointer"
        @selection-change="handleSelectionChange"
        @row-dblclick="
          row => {
            goTo(
              props.businessType == 'PTCG'
                ? '/source/purchaseOrder/purchaseInboundForm'
                : '/source/innerOrder/innerInboundForm',
              {
                id: row.id
              }
            )
          }
        "
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          v-for="(item, index) in tableCol.filter(item => item.show)"
          :key="index"
          v-bind="item"
        >
          <template #default="scope">
            <div v-if="item.dict">
              <dict-drop
                v-if="item.dict"
                :options="item.dict"
                :value="scope.row[item.prop]"
              />
            </div>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
      <!-- 
        :before-close="handleClose" -->
      <el-dialog v-model="dialogVisible" title="显示/隐藏列">
        <!-- <fix-data v-model="tableCol" /> -->
        <el-transfer
          :titles="['显示', '隐藏']"
          v-model="tableval"
          :data="tableColumns"
          @change="dataChange"
          class="text-center"
      /></el-dialog>

      <!-- <FixTable v-model="tableCol" /> -->
    </div>
  </div>
</template>
<script setup name="OrderInboundTable">
import { useRouter, useRoute } from 'vue-router'
import { listOrderInbound } from '@/api/indentManage/indent'
import { parseTime } from '@/utils/ruoyi'
import {
  companyOption //归属公司
} from '@/hooks/dict/order'
import useUserStore from '@/store/modules/user'
const userStore = useUserStore()
const router = useRouter()
const route = useRoute()
const { proxy, type } = getCurrentInstance()

const props = defineProps({
  businessType: {
    type: [Number, String],
    require: false
  }
})

const { order_property } = proxy.useDict(
  'order_property' //-- 订单属性
)

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    unit: userStore.userInfo.orgId,
    supplierName: undefined,
    supplierTaxcode: undefined,
    expireDate: undefined
  },
  ids: [],
  selectArr: [],
  total: 0,
  option: {
    labelWidth: '10',
    menuSpan: 6,
    submitBtn: false,
    menuPosition: 'left',
    // gutter: 30,
    emptyBtn: false,
    column: [
      {
        prop: 'customerName',
        span: 3,
        placeholder: '供应商名称',
        type: 'input'
      },
      {
        prop: 'number',
        span: 3,
        placeholder: '单据编号',
        type: 'input'
      },
      {
        prop: 'unit',
        span: 3,
        placeholder: '所属公司',
        type: 'tree',
        defaultExpandAll: true,
        disabled: userStore.userInfo.orgId != 484,
        props: {
          value: 'id', //   value: 'deptId',
          label: 'label' //   label: 'deptName'
        },
        dicData: companyOption
      },
      {
        prop: 'inboundStatus',
        span: 3,
        placeholder: '入库情况',
        type: 'select',
        props: {
          value: 'id', //   value: 'deptId',
          label: 'label' //   label: 'deptName'
        },
        dicData: [
          {
            label: '入库成功',
            id: 1
          },
          {
            label: '入库失败',
            id: 2
          }
        ]
      },
      {
        prop: 'receiptsDateArr',
        type: 'daterange',
        span: 5,
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
        startPlaceholder: '单据开始时间',
        endPlaceholder: '单据结束时间'
      }
      // {
      //   prop: 'supplierTaxcode',
      //   span: 3,
      //   placeholder: '供应商税号',
      //   type: 'input'
      // },
    ]
  },
  showSearch: true,
  disUpdate: true /* 编辑按钮是否可编辑 */,
  disAssess: true /* 评审是否可编辑 */,
  disDelete: true /* 关闭按钮是否可编辑 */,
  loading: false,
  tableList: [],
  followUpParam: {} // 跟进弹窗配置
})
const {
  queryParams,
  ids,
  selectArr,
  total,
  option,
  showSearch,
  disUpdate,
  disAssess,
  disDelete,
  loading,
  tableList,
  followUpParam
} = toRefs(data)

watch(
  [() => props.businessType],
  async () => {
    if (props.businessType) {
      queryParams.value.businessType = props.businessType
    }
  },
  {
    immediate: true
  }
)
const tableConfig = [
  {
    show: true,
    prop: 'property',
    label: '单据属性',
    align: 'center',
    dict: order_property,
    minWidth: 80,
    showOverflowTooltip: true
  },
  {
    show: true,
    prop: 'customerName',
    label: '供应商名称',
    align: 'center',
    minWidth: 150,
    showOverflowTooltip: true
  },
  {
    show: false,
    prop: 'customerNumber',
    label: '供应商编号',
    align: 'center',
    minWidth: 80,
    showOverflowTooltip: true
  },
  {
    show: true,
    prop: 'number',
    label: '单据编号',
    align: 'center',
    minWidth: 80,
    showOverflowTooltip: true
  },
  {
    show: userStore.userInfo.orgId == 484,
    prop: 'orgName',
    label: '所属公司',
    align: 'center',
    minWidth: 80,
    showOverflowTooltip: true
  },
  {
    show: true,
    prop: 'receiptsDate',
    label: '单据日期',
    align: 'center',
    formatter: row => {
      return parseTime(row.receiptsDate, '{y}-{m}-{d}')
    },
    minWidth: 80,
    showOverflowTooltip: true
  },
  {
    show: true,
    prop: 'warehouseName',
    label: '仓库名字',
    align: 'center',
    minWidth: 80,
    showOverflowTooltip: true
  },
  {
    show: true,
    prop: 'inboundStatus',
    label: '入库状态',
    align: 'center',
    minWidth: 80,
    showOverflowTooltip: true
  },
  {
    show: false,
    prop: 'warehouseNumber',
    label: '仓库编码',
    align: 'center',
    minWidth: 80,
    showOverflowTooltip: true
  },
  {
    show: false,
    prop: 'remark',
    label: '备注',
    align: 'center',
    minWidth: 80,
    showOverflowTooltip: true
  }
]
// const orderInbound = JSON.parse(localStorage.getItem('orderInbound'))
// localStorage.setItem('orderInbound', JSON.stringify(tableConfig))
// localStorage.removeItem('orderInbound')
// console.log('orderInbound==>', orderInbound)

const tableval = ref([])
const tableColumns = computed(() => {
  return tableConfig.map(item => {
    return {
      key: item.prop,
      label: item.label,
      disabled: false
    }
  })
})
const tableCol = ref(tableConfig)
const dataChange = val => {
  tableCol.value = tableCol.value.map(item => {
    item.show = !val.includes(item.prop)
    return item
  })
}
const dialogVisible = ref(false)

// 初始化
async function init() {
  tableConfig.forEach(item => {
    if (!item.show) tableval.value.push(item.prop)
  })
  // 归属部门
  getList()
}
// 重置
function handleEmpty(formEl) {
  console.log('queryParams.value1==>', toRaw(queryParams.value))
  proxy.resetForm('searchForm')
  proxy.resetForm('seniorSearchForm')
  // proxy.$refs.searchForm.resetForm()
  getList()
}

// 获取数据列表
function getList() {
  loading.value = true
  console.log('queryParams.value2==>', queryParams.value)
  listOrderInbound(queryParams.value).then(res => {
    loading.value = false
    tableList.value = res.rows
    total.value = res.total
    console.log(' tableList.value==>', tableList.value)
  })
  console.log('获取数据')
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  selectArr.value = selection
  let approvalUpdate = selectArr.value.some(
    item => item.approvalStatus == '流程未发起'
  )
  let approvalStatus = selectArr.value.some(
    item => item.approvalStatus == '审批通过'
  )
  disUpdate.value = selection.length != 1 || !approvalUpdate
  disAssess.value = selection.length != 1 || !approvalStatus
  disDelete.value = !selection.length
}
function goTo(url, data) {
  router.push({ path: url, query: data })
}

init()
</script>
<style scoped>
.search-drawer-retract {
  color: #999999;
}
</style>
